package com.csust.automaticteach.manager.cos;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.RandomUtil;
import com.csust.automaticteach.config.CosClientConfig;
import com.csust.automaticteach.constant.FileConstant;
import com.csust.automaticteach.exception.BusinessException;
import com.csust.automaticteach.exception.ErrorCode;
import com.csust.automaticteach.exception.ThrowUtils;
import com.csust.automaticteach.model.dto.file.UploadFileDetail;
import com.qcloud.cos.model.PutObjectResult;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.Date;

import static com.csust.automaticteach.constant.FileConstant.*;


/**
 * @ClassNameFileManager
 * @Description 文件上传统一的方法
 * @Author钰玟
 * @Date2025/4/17 下午3:11
 * @Version 1.0
 **/

@Service
@Slf4j
public class FileManager {

    @Resource
    private CosClientConfig cosClientConfig;

    @Resource
    private CosManager cosManager;

    /**
     * 上传图片
     *
     * @param multipartFile    文件
     * @param uploadPathPrefix 上传路径前缀
     * @return 图片详细信息
     */
    public UploadFileDetail uploadFile(MultipartFile multipartFile, String uploadPathPrefix) {
        //校验图片
        validFile(multipartFile);
        //图片上传地址的处理
        String uuid = RandomUtil.randomNumbers(16);
        //文件图片名称
        String originalFilename = multipartFile.getOriginalFilename();

        String fileSuffix = FileUtil.getSuffix(originalFilename);
        if (fileSuffix.endsWith("doc")) {
            fileSuffix = fileSuffix + "x";
        }
        // DateUtil.formatDate(new Date()) 创建日期  uuid ：uuid  FileUtil.getSuffix(originalFilename))：后缀名 例如 : .doc  .docx .md
        String uploadFileName = String.format("%s_%s.%s", DateUtil.formatDate(new Date()), uuid, fileSuffix);
        String uploadPath = String.format("/%s/%s", uploadPathPrefix, uploadFileName);
        File file = null;
        try {
            //创建临时文件
            file = File.createTempFile(uploadPath, null);
            multipartFile.transferTo(file);
            //上传文件
            PutObjectResult putObjectResult = cosManager.putPictureObject(uploadPath, file);
            //封装返回结果
            UploadFileDetail uploadPictureDetail = new UploadFileDetail();
            //文件大小
            uploadPictureDetail.setFileSize(FileUtil.size(file));
            //拼接文件访问地址
            uploadPictureDetail.setUrl(cosClientConfig.getHost() + "/" + uploadPath);
            return uploadPictureDetail;
        } catch (Exception e) {
            log.error("fail to upload the file,the reason is:" + e.getMessage());
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, FileConstant.FAIL_UPLOAD);
        } finally {
            this.deleteTemFile(file);
        }
    }

    /**
     * 校验图片文件是否合法
     *
     * @param multipartFile 图片文件
     */
    private void validFile(MultipartFile multipartFile) {
        ThrowUtils.throwIf(multipartFile == null, ErrorCode.PARAMS_ERROR, FILE_CAN_NOT_BE_NULL);
        //1.校验文件大小
        long fileSize = multipartFile.getSize();
        //不能超过 10M
        ThrowUtils.throwIf(fileSize > 10 * ONE_M, ErrorCode.PARAMS_ERROR, FILE_SIZE_ERROR);
        //2. 校验文件后缀
        String fileSuffix = FileUtil.getSuffix(multipartFile.getOriginalFilename());
        ThrowUtils.throwIf(!ALLOW_FILE_SUFFIX.contains(fileSuffix), ErrorCode.PARAMS_ERROR, FILE_TYPE_ERROR);
    }

    /**
     * 删除临时文件
     *
     * @param file 文件
     */
    private void deleteTemFile(File file) {
        if (file == null) {
            return;
        }
        //删除临时文件
        boolean deleteResult = file.delete();
        if (!deleteResult) {
            log.error("file delete fail,filePath:{}", file.getAbsolutePath());
        }
    }
}

